package com.iss.components.menubar;

import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.List;

import javax.servlet.jsp.JspException;

/**
 * @author zhouyi
 *
 */
@SuppressWarnings("serial")
public class MenuTag extends BaseMenuTag{

	private String clazz = null; 
	private boolean separator = true;
	private String id = null;

	@Override
	public int doStartTag() throws JspException {
		item = new MenuItem();
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		if (item!=null&&item.getItems().size()>0){
			try{
				Writer writer = pageContext.getOut();
				StringBuffer html = new StringBuffer();
				String idStr = id;
				if (idStr==null||idStr.trim().length()<=0) idStr = generateMenuUniqueId();
				html.append("<div id='"+idStr+"' class='" + clazz + "'>");
				html.append(getMenuHtml(item.getItems(),true));		
				html.append("<div style='clear:left'></div></div>");
				html.append("<script>$('#"+idStr+"').menu()</script>");
				writer.write(html.toString());
			}catch(IOException e){}
		}
		item = null ;
		return EVAL_PAGE;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public boolean isSeparator() {
		return separator;
	}

	public void setSeparator(boolean spliter) {
		this.separator = spliter;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	private String getMenuHtml(List <MenuItem> menuItems , boolean isTopMenu){
		if (menuItems == null || menuItems.size()<=0) return "";
		StringBuffer html = new StringBuffer();
		html.append("<ul>");
		for (int i = 0; i < menuItems.size(); i++) {
			MenuItem item = menuItems.get(i);
			String imgStr = item.getImg();
			String hrefStr = item.getHref();
			String textStr = item.getText();
			String clickStr = item.getClick();
	
			boolean hasList = item.getItems()!=null&&item.getItems().size()>0;
			String listStr = hasList?"list":"";
			String focusStr = item.isFocus()?"focus":"";
			String classStr = "";
			if (!(focusStr==""&&listStr=="")){
				classStr = "class='"+focusStr+" "+listStr+"'";
			}
			
			imgStr = (imgStr==null?"":"<img src='"+imgStr+"' />");
			hrefStr = (hrefStr==null?"":"href='"+hrefStr+"'");
			textStr = (textStr==null?"":textStr);
			clickStr = (clickStr==null?"":"onclick='"+clickStr+"'");
			
			html.append("<li>");
			html.append(imgStr).append("<a "+classStr+" "+hrefStr+" "+clickStr+">"+textStr+"</a>");
			html.append(getMenuHtml(item.getItems(),false));
			html.append("</li>");
			if (isTopMenu){
				if ((!item.isFocus()) && 
					!((menuItems.size()>i+1)&&menuItems.get(i+1).isFocus()))
					if(separator) html.append("<li><a class='separator'>|</a></li>");
			}
		}
		html.append("</ul>");
		return html.toString();
	}
	
	private static String generateMenuUniqueId(){
		String millis = String.valueOf(System.currentTimeMillis());
		return "imenu-"+millis.substring(9)+(int)(Math.random()*1000);
	}
	
	public static void main(String[] args){
		String str = generateMenuUniqueId();
		String str2 = generateMenuUniqueId();
		System.out.println(str);
		System.out.println(str2);
	}
	
}
